package com.wtwd.campus.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.dao.FenceInfoMapper;
import com.wtwd.campus.entity.FenceInfo;
import com.wtwd.campus.entity.H5Fence;
import com.wtwd.campus.service.FenceInfoService;
import com.wtwd.campus.utils.GouldUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FenceInfoServiceImpl implements FenceInfoService {
    @Autowired
    private FenceInfoMapper fenceInfoMapper;
    @Autowired
    private GouldUtil gouldUtil;
    @Override
    public Integer addFence(FenceInfo fenceInfo) {
        //获取中心点位置
        String location = null;
        try {
            location = gouldUtil.getAMapByLngAndLat(fenceInfo.getLongitude().toString(), fenceInfo.getLatitude().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        fenceInfo.setLocation(location);
        fenceInfo.setCreate_time(System.currentTimeMillis());
        Integer i = fenceInfoMapper.addFence(fenceInfo);
        return i;
    }

    @Override
    public Map<String, Object> queryFenceList(String orgCode, String name, Integer currPage, Integer pageSize) {
        Page<Map<String,Object>> page=new Page<>(currPage,pageSize);
        IPage<Map<String,Object>> result = fenceInfoMapper.getFenceInfoList(orgCode, name,page);
        return disposalData(result);
    }

    @Override
    public Integer modifyFenceInfo(FenceInfo fenceInfo) {
        //获取中心点位置
        String location = null;
        try {
            location = gouldUtil.getAMapByLngAndLat(fenceInfo.getLongitude().toString(), fenceInfo.getLatitude().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        fenceInfo.setLocation(location);
        return fenceInfoMapper.modifyFenceInfo(fenceInfo);
    }

    @Override
    public Integer deleteFenceInfo(Integer fenceId) {
        return fenceInfoMapper.deleteFenceIfo(fenceId);
    }

    @Override
    public List<FenceInfo> getFenceInfoByDeviceId(Integer deviceId) {
        return fenceInfoMapper.getFenceInfoByDeviceId(deviceId);
    }

    @Override
    public List<FenceInfo> getFenceInfoListByStuId(Integer stuId) {
        return fenceInfoMapper.getFenceInfoListByStuId(stuId);
    }

    @Override
    public FenceInfo getFenceInfoById(Integer id) {
        return fenceInfoMapper.getFenceInfoById(id);
    }

    @Override
    public Integer addWxFence(FenceInfo fenceInfo) {
        if(StringUtils.isBlank(fenceInfo.getLocation())){
            //获取中心点位置
            String location = null;
            try {
                location = gouldUtil.getAMapByLngAndLat(fenceInfo.getLongitude().toString(), fenceInfo.getLatitude().toString());
                fenceInfo.setLocation(location);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        fenceInfo.setCreate_time(System.currentTimeMillis());
        Integer i = fenceInfoMapper.addFence(fenceInfo);
        return i;
    }

    @Override
    public Integer modifyWxFenceInfo(FenceInfo fenceInfo) {
        if(StringUtils.isBlank(fenceInfo.getLocation())) {
            //获取中心点位置
            String location = null;
            try {
                location = gouldUtil.getAMapByLngAndLat(fenceInfo.getLongitude().toString(), fenceInfo.getLatitude().toString());
                fenceInfo.setLocation(location);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return fenceInfoMapper.modifyFenceInfo(fenceInfo);
    }

    @Override
    public List<H5Fence> getH5FenceByDeviceId(Integer deviceId) {
        return fenceInfoMapper.getH5FenceByDeviceId(deviceId);
    }

    @Override
    public H5Fence getH5FenceById(Integer id) {
        return fenceInfoMapper.getH5FenceById(id);
    }

    @Override
    public List<H5Fence> getH5FenceByImei(String imei) {
        return fenceInfoMapper.getH5FenceByImei(imei);
    }

    @Override
    public Integer deleteH5FenceById(Integer id) {
        return fenceInfoMapper.deleteH5FenceById(id);
    }

    @Override
    public H5Fence getH5FenceByCusIdAndId(Integer userId, Integer id) {
        return fenceInfoMapper.getH5FenceByCusIdAndId(userId, id);
    }

    @Override
    public Integer modifyH5FenceById(H5Fence h5Fence) {
        return fenceInfoMapper.modifyH5FenceById(h5Fence);
    }

    @Override
    public Integer addH5Fence(H5Fence h5Fence) {
        if(StringUtils.isBlank(h5Fence.getAddress())){
            //获取中心点位置
            String location = null;
            try {
                location = gouldUtil.getAMapByLngAndLat(h5Fence.getLongitude().toString(), h5Fence.getLatitude().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
            h5Fence.setAddress(location);
        }
        return fenceInfoMapper.addH5Fence(h5Fence);
    }

    /**
     * 构建返回数据
     *
     * @return
     */
    public <T> Map<String, Object> disposalData(IPage<T> iPage) {
        Map<String, Object> map = new HashMap<>();
        List<T> records = iPage.getRecords();
        long total = iPage.getTotal();
        map.put("total", total);
        map.put("list", records);
        return map;
    }

}
